<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:db="http://docbook.org/ns/docbook"
  xmlns:pbs="http://schema.pbs.gov.au" xmlns:rwt="http://schema.pbs.gov.au/RWT/"
  xmlns:dct="http://purl.org/dc/terms/">

  <db:article>
    <db:info>
      <db:title>Support Module</db:title>

      <db:copyright>
        <db:year>2013</db:year>
        <db:year>2012</db:year>
        <db:year>2011</db:year>
        <db:year>2009</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").
          The following terms apply to all files associated with the software unless explicitly
          disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this
          software and its documentation for any purpose, provided that existing copyright notices
          are retained in all copies and that this notice is included verbatim in any distributions.
          No written agreement, license, or royalty fee is required for any of the authorized uses.
          Modifications to this software may be copyrighted by their authors and need not follow the
          licensing terms described here, provided that the new terms are clearly indicated on the
          first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
          DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
          THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE
          BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED
          TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
          NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS
          NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
          MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>2.12.1</db:revnumber>
          <db:date>2013-11-05</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Define 'ident' key.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.12</db:revnumber>
          <db:date>2013-10-31</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Make ASCII translation template a named template.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.11</db:revnumber>
          <db:date>2013-07-17</db:date>
          <db:authorinitials>JPK</db:authorinitials>
          <db:revremark>Include rwt:prescriber-instruction content into restrictions. Remove rwt:prescriber-instruction content from notes.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.10</db:revnumber>
          <db:date>2012-11-01</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Added default parameter to find-latest template.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.9</db:revnumber>
          <db:date>2012-10-23</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Added get-default-dispensing-rule template.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.8</db:revnumber>
          <db:date>2012-08-29</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Added find-latest template.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.7</db:revnumber>
          <db:date>2012-08-20</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Improved date related templates.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.6.2</db:revnumber>
          <db:date>2012-08-29</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Added &#8211; character to text translation.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.6.1</db:revnumber>
          <db:date>2012-08-14</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Added tab to textonly translation.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.6</db:revnumber>
          <db:date>2012-07-09</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Added format-date-minus-1D template.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.5.1</db:revnumber>
          <db:date>2012-05-29</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Added newline to textonly translation.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.5</db:revnumber>
          <db:date>2012-05-15</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Added expand-qnames template.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.4</db:revnumber>
          <db:date>2011-10-26</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Added format-date-buggy template.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.3</db:revnumber>
          <db:date>2011-08-01</db:date>
          <db:authorinitials>DMM</db:authorinitials>
          <db:revremark>Added translations to convert certain unicode characters to ascii
            equiavelents.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.2</db:revnumber>
          <db:date>2011-06-21</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Added pad parameter to pad-number template.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.1</db:revnumber>
          <db:date>2011-06-15</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Added truncate template. Fixed date format.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>2.0</db:revnumber>
          <db:date>2011-02-14</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Updated for PBS XML Schema v2.4.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2009-10-02</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This XSL stylesheet is a support module for the XSL stylesheets that produce the
      Standard and ANS extracts. Do not use this stylesheet by itself to transform
      documents.</db:para>
  </db:article>
  
  <xsl:variable name='dr.rp.cp'>http://schema.pbs.gov.au/DR/RP/s90-cp</xsl:variable>
  <xsl:variable name='dr.rp.cp.qn'>dr:RP/s90-cp</xsl:variable>
  <xsl:variable name='dr.rp-np.cp'>http://schema.pbs.gov.au/DR/RP-NP/s90-cp</xsl:variable>
  <xsl:variable name='dr.rp-np.cp.qn'>dr:RP-NP/s90-cp</xsl:variable>
  <xsl:variable name='dr.rp-np-nc.cp'>http://schema.pbs.gov.au/DR/RP-NP-NC/s90-cp</xsl:variable>
  <xsl:variable name='dr.rp-np-nc.cp.qn'>dr:RP-NP-NC/s90-cp</xsl:variable>
  <xsl:variable name='dr.rp-np-nc.ds'>http://schema.pbs.gov.au/DR/RP-NP-NC/ds-assoc</xsl:variable>
  <xsl:variable name='dr.rp-np-nc.ds.qn'>dr:RP-NP-NC/ds-assoc</xsl:variable>
  <xsl:variable name='dr.rp-np-nc.dm'>http://schema.pbs.gov.au/DR/RP-NP-NC/ds-manu</xsl:variable>
  <xsl:variable name='dr.rp-np-nc.dm.qn'>dr:RP-NP-NC/ds-manu</xsl:variable>
  <xsl:variable name='dr.rp.private'>http://schema.pbs.gov.au/DR/RP/s94-private</xsl:variable>
  <xsl:variable name='dr.rp.private.qn'>dr:RP/s94-private</xsl:variable>
  <xsl:variable name='dr.rp.public'>http://schema.pbs.gov.au/DR/RP/s94-public</xsl:variable>
  <xsl:variable name='dr.rp.public.qn'>dr:RP/s94-public</xsl:variable>
  
  <xsl:variable name='dr.rp.private.s100'>http://schema.pbs.gov.au/DR/RP/s94-private-s100</xsl:variable>
  <xsl:variable name='dr.rp.private.s100.qn'>dr:RP/s94-private-s100</xsl:variable>
  <xsl:variable name='dr.rp.public.s100'>http://schema.pbs.gov.au/DR/RP/s94-public-s100</xsl:variable>
  <xsl:variable name='dr.rp.public.s100.qn'>dr:RP/s94-public-s100</xsl:variable>
  
  <xsl:variable name='dr.in.cp'>http://schema.pbs.gov.au/DR/IN/s90-cp</xsl:variable>
  <xsl:variable name='dr.in.cp.qn'>dr:IN/s90-cp</xsl:variable>
  <xsl:variable name='dr.in.private'>http://schema.pbs.gov.au/DR/IN/s94-private</xsl:variable>
  <xsl:variable name='dr.in.private.qn'>dr:IN/s94-private</xsl:variable>
  <xsl:variable name='dr.in.public'>http://schema.pbs.gov.au/DR/IN/s94-public</xsl:variable>
  <xsl:variable name='dr.in.public.qn'>dr:IN/s94-public</xsl:variable>
  <xsl:variable name='dr.in-sa.cp'>http://schema.pbs.gov.au/DR/IN/s90-cp-sa</xsl:variable>
  <xsl:variable name='dr.in-sa.cp.qn'>dr:IN/s90-cp-sa</xsl:variable>
  <xsl:variable name='dr.in-sa.private'>http://schema.pbs.gov.au/DR/IN/s94-private-sa</xsl:variable>
  <xsl:variable name='dr.in-sa.private.qn'>dr:IN/s94-private-sa</xsl:variable>
  <xsl:variable name='dr.in-sa.public'>http://schema.pbs.gov.au/DR/IN/s94-public</xsl:variable>
  <xsl:variable name='dr.in-sa.public.qn'>dr:IN/s94-public</xsl:variable>

  <xsl:key name="ident" match="*" use="@xml:id"/>

  <xsl:template name="get-default-dispensing-rule">
    <xsl:param name="qnames"/>
    <xsl:param name="program"/>

    <xsl:choose>
      <xsl:when test='$qnames and
        ($program = "HB" or
        $program = "SZ" or
        $program = "CT")'>
        <xsl:value-of select='$dr.rp.public.s100.qn'/>
      </xsl:when>
      <xsl:when test='$program = "HB" or
        $program = "SZ" or
        $program = "CT"'>
        <xsl:value-of select='$dr.rp.public.s100'/>
      </xsl:when>
      <xsl:when test='$qnames and
        ($program = "HS" or
        $program = "SY")'>
        <xsl:value-of select='$dr.rp.private.s100.qn'/>
      </xsl:when>
      <xsl:when test='$program = "HS" or
        $program = "SY"'>
        <xsl:value-of select='$dr.rp.private.s100'/>
      </xsl:when>
      <xsl:when test='$qnames and
        $program = "DB"'>
        <xsl:value-of select='$dr.rp-np-nc.cp.qn'/>
      </xsl:when>
      <xsl:when test='$program = "DB"'>
        <xsl:value-of select='$dr.rp-np-nc.cp'/>
      </xsl:when>
      <xsl:when test='$qnames and
        $program = "R1"'>
        <xsl:value-of select='$dr.rp-np.cp.qn'/>
      </xsl:when>
      <xsl:when test='$program = "R1"'>
        <xsl:value-of select='$dr.rp-np.cp'/>
      </xsl:when>
      <xsl:when test='$qnames and
        $program = "PQ"'>
        <xsl:value-of select='$dr.rp-np-nc.ds.qn'/>
      </xsl:when>
      <xsl:when test='$program = "PQ"'>
        <xsl:value-of select='$dr.rp-np-nc.ds'/>
      </xsl:when>
      <xsl:when test='$qnames and
        ($program = "GH" or
        $program = "IF" or
        $program = "MD" or
        $program = "MF")'>
        <xsl:value-of select='$dr.rp-np-nc.dm.qn'/>
      </xsl:when>
      <xsl:when test='$program = "GH" or
        $program = "IF" or
        $program = "MD" or
        $program = "MF"'>
        <xsl:value-of select='$dr.rp-np-nc.dm'/>
      </xsl:when>
      <xsl:when test='$qnames and
        $program = "IP"'>
        <xsl:value-of select='$dr.in.public.qn'/>
      </xsl:when>
      <xsl:when test='$program = "IP"'>
        <xsl:value-of select='$dr.in.public'/>
      </xsl:when>
      <xsl:when test='$qnames and
        $program = "IN"'>
        <xsl:value-of select='$dr.in.cp.qn'/>
      </xsl:when>
      <xsl:when test='$program = "IN"'>
        <xsl:value-of select='$dr.in.cp'/>
      </xsl:when>
      <xsl:when test='$qnames and
        $program = "TZ"'>
        <xsl:value-of select='$dr.in-sa.public.qn'/>
      </xsl:when>
      <xsl:when test='$program = "TZ"'>
        <xsl:value-of select='$dr.in-sa.public'/>
      </xsl:when>
      <xsl:when test='$qnames and
        $program = "TY"'>
        <xsl:value-of select='$dr.in-sa.cp.qn'/>
      </xsl:when>
      <xsl:when test='$program = "TY"'>
        <xsl:value-of select='$dr.in-sa.cp'/>
      </xsl:when>
      <xsl:when test='$qnames'>
        <xsl:value-of select='$dr.rp.cp.qn'/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select='$dr.rp.cp'/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- 'expand-qnames' takes nodeset containing elements that contain qnames.
       Each qname is expanded to its URI equivalent and returned as a space-separated list.
    -->
  <xsl:template name="expand-qnames">
    <xsl:param name="qnames"/>

    <xsl:choose>
      <xsl:when test="not($qnames)"/>
      <xsl:when
        test="not(contains($qnames[1], ':')) or
                      starts-with($qnames[1], 'http:') or
                      starts-with($qnames[1], 'urn:')">
        <xsl:value-of select="$qnames[1]"/>
        <xsl:text> </xsl:text>

        <xsl:call-template name="expand-qnames">
          <xsl:with-param name="qnames" select="$qnames[position() != 1]"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="prefix" select="substring-before($qnames[1], ':')"/>
        <xsl:variable name="local-name" select="substring-after($qnames[1], ':')"/>

        <xsl:value-of select="namespace::*[name() = $prefix]"/>
        <xsl:value-of select="$local-name"/>
        <xsl:text> </xsl:text>

        <xsl:call-template name="expand-qnames">
          <xsl:with-param name="qnames" select="$qnames[position() != 1]"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Convert certain unicode characters to their [rough] ascii equivalents -->
  <xsl:template match="text()" mode="textonly" name="ascii">
    <xsl:param name="text" select="."/>
    <xsl:value-of select="translate($text, '&#xa0;&#x2014;&#xe9;&#xea;&#xf4;&#xa;&#09;&#8211;', ' -eeo  -')"/>
  </xsl:template>

  <xsl:variable name="ns.fee" select="'http://schema.pbs.gov.au/Fee#'"/>

  <xsl:template name="fee-to-text">
    <xsl:param name="uri"/>

    <xsl:choose>
      <xsl:when test="$uri = 'fee:dispensing'">RP</xsl:when>
      <xsl:when test="$uri = concat($ns.fee, 'dispensing')">RP</xsl:when>
      <xsl:when test="$uri = 'fee:water-added'">EW</xsl:when>
      <xsl:when test="$uri = concat($ns.fee, 'water-added')">EW</xsl:when>
      <xsl:when test="$uri = 'fee:dangerous-drug'">DD</xsl:when>
      <xsl:when test="$uri = concat($ns.fee, 'dangerous-drug')">DD</xsl:when>
      <xsl:when test="$uri = 'fee:extemp'">EP</xsl:when>
      <xsl:when test="$uri = concat($ns.fee, 'extemp')">EP</xsl:when>
      <xsl:when test="$uri = 'fee:none'">NF</xsl:when>
      <xsl:when test="$uri = concat($ns.fee, 'none')">NF</xsl:when>
      <xsl:otherwise>
        <xsl:text>  </xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <db:template name="truncate">
    <db:para>Truncate a string to a specified length.</db:para>
  </db:template>
  <xsl:template name="truncate">
    <xsl:param name="text"/>
    <xsl:param name="length" select="1"/>

    <!-- Due to the limitations of XSLT 1.0, we can't pass through a string value to the template that contains
           the translation of unicode character, so it has to be duplicated here -->
    <xsl:value-of
      select="translate(substring($text, 1, $length), '&#xa0;&#x2014;&#xe9;&#xea;&#xf4;&#xa;&#09;&#8211;', ' -eeo  -')"
    />
  </xsl:template>
  <db:template name="pad-right">
    <db:para>Pad a string on the right with spaces.</db:para>
  </db:template>
  <xsl:template name="pad-right">
    <xsl:param name="text"/>
    <xsl:param name="length" select="1"/>

    <!-- Due to the limitations of XSLT 1.0, we can't pass through a string value to the template that contains
           the translation of unicode character, so it has to be duplicated here -->
    <xsl:value-of select="translate($text,'&#xa0;&#x2014;&#xe9;&#xea;&#xf4;',' -eeo')"/>
    <xsl:if test="string-length($text) &lt; $length">
      <xsl:call-template name="repeat">
        <xsl:with-param name="what" select="' '"/>
        <xsl:with-param name="number" select="$length - string-length($text)"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  <db:template name="pad-number">
    <db:para>Pad a number on the left. Default padding character is space.</db:para>
  </db:template>
  <xsl:template name="pad-number">
    <xsl:param name="number"/>
    <xsl:param name="length" select="1"/>
    <xsl:param name="pad" select="' '"/>

    <xsl:if test="string-length($number) &lt; $length">
      <xsl:call-template name="repeat">
        <xsl:with-param name="what" select="$pad"/>
        <xsl:with-param name="number" select="$length - string-length($number)"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:value-of select="$number"/>
  </xsl:template>
  <db:template name="repeat">
    <db:para>Duplicate a string some number of times.</db:para>
    <db:para>NB. Use an algorithm that avoids problems with tail-end recursion.</db:para>
  </db:template>
  <xsl:template name="repeat">
    <xsl:param name="what"/>
    <xsl:param name="number" select="0"/>

    <xsl:choose>
      <xsl:when test="$number &lt; 1"/>
      <xsl:when test="$number div 100 > 1">
        <xsl:call-template name="repeat">
          <xsl:with-param name="what">
            <xsl:call-template name="repeat">
              <xsl:with-param name="what" select="$what"/>
              <xsl:with-param name="number" select="100"/>
            </xsl:call-template>
          </xsl:with-param>
          <xsl:with-param name="number" select="$number div 100"/>
        </xsl:call-template>
        <xsl:call-template name="repeat">
          <xsl:with-param name="what" select="$what"/>
          <xsl:with-param name="number" select="$number mod 100"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$what"/>
        <xsl:call-template name="repeat">
          <xsl:with-param name="what" select="$what"/>
          <xsl:with-param name="number" select="$number - 1"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <db:template name="format-date">
    <db:para>Re-format a date value in the format required by the text extracts.</db:para>
  </db:template>
  <xsl:template name="format-date">
    <xsl:param name="date"/>

    <xsl:value-of select="substring($date, 9, 2)"/>
    <xsl:value-of select="substring($date, 6, 2)"/>
    <xsl:value-of select="substring($date, 1, 4)"/>
  </xsl:template>

  <db:template name="format-date-buggy">
    <db:para>Re-format a date value in the format required by the old-style text extracts.</db:para>
  </db:template>
  <xsl:template name="format-date-buggy">
    <xsl:param name="date"/>

    <xsl:value-of select="format-number(substring($date, 9, 2), '0')"/>
    <xsl:value-of select="substring($date, 6, 2)"/>
    <xsl:value-of select="substring($date, 1, 4)"/>
  </xsl:template>

  <db:template name="format-date-minus-1D">
    <db:para>Subtract 1 day from a date and re-format the date value in the format required by the
      text extracts.</db:para>
  </db:template>
  <xsl:template name="format-date-minus-1D">
    <xsl:param name="date"/>
    <xsl:param name="buggy" select="'no'"/>

    <xsl:variable name="year" select="substring-before($date, '-')"/>
    <xsl:variable name="month" select="substring(substring-after($date, '-'), 1, 2)"/>
    <xsl:variable name="day" select="substring-after(substring-after($date, '-'), '-')"/>

    <xsl:variable name="new-day">
      <xsl:choose>
        <xsl:when test="$day != '01'">
          <xsl:value-of select="$day - 1"/>
        </xsl:when>
        <xsl:when test="$month = '01'">31</xsl:when>
        <xsl:when test="$month = '02'">31</xsl:when>
        <xsl:when test="$month = '04'">31</xsl:when>
        <xsl:when test="$month = '05'">30</xsl:when>
        <xsl:when test="$month = '06'">31</xsl:when>
        <xsl:when test="$month = '07'">30</xsl:when>
        <xsl:when test="$month = '08'">31</xsl:when>
        <xsl:when test="$month = '09'">30</xsl:when>
        <xsl:when test="$month = '10'">30</xsl:when>
        <xsl:when test="$month = '11'">31</xsl:when>
        <xsl:when test="$month = '12'">30</xsl:when>
        <xsl:otherwise>
          <!-- February -->
          <!-- Is this a leap year? -->
          <xsl:choose>
            <xsl:when test="$year mod 400 = 0">29</xsl:when>
            <xsl:when test="$year mod 100 = 0">28</xsl:when>
            <xsl:when test="$year mod 4 = 0">29</xsl:when>
            <xsl:otherwise>28</xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="new-month">
      <xsl:choose>
        <xsl:when test="$day != '01'">
          <xsl:value-of select="$month"/>
        </xsl:when>
        <xsl:when test="$month = '01'">12</xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$month - 1"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="new-year">
      <xsl:choose>
        <xsl:when test="$day != '01'">
          <xsl:value-of select="$year"/>
        </xsl:when>
        <xsl:when test="$month = '01'">
          <xsl:value-of select="$year - 1"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$year"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="$buggy = 'no'">
        <xsl:call-template name="format-date-buggy">
          <xsl:with-param name="date" select="concat($new-year, '-', format-number($new-month, '00'), '-', format-number($new-day, '00'))"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="format-date">
          <xsl:with-param name="date" select="concat($new-year, '-', format-number($new-month, '00'), '-', format-number($new-day, '00'))"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <!-- Given a space-delimited list of dates, return the latest date -->
  <xsl:template name="find-latest">
    <xsl:param name="dates"/>
    <xsl:param name="default"/>

    <xsl:variable name="dates.cooked" select="normalize-space($dates)"/>
    <xsl:choose>
      <xsl:when test="$dates.cooked = ''">
        <xsl:value-of select="$default"/>
      </xsl:when>
      <xsl:when test="not(contains($dates.cooked, ' '))">
        <xsl:value-of select="$dates.cooked"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="first" select="substring-before($dates.cooked, ' ')"/>
        <xsl:variable name="rest">
          <xsl:call-template name="find-latest">
            <xsl:with-param name="dates" select="substring-after($dates.cooked, ' ')"/>
          </xsl:call-template>
        </xsl:variable>
        
        <xsl:choose>
          <xsl:when test="translate($first, '-', '') > translate($rest, '-', '')">
            <xsl:value-of select="$first"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$rest"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="rwt:restriction-reference |
    rwt:circumstance-reference |
    rwt:administrative-advice-reference |
    rwt:definition-reference |
    rwt:caution-reference" mode="note-start">
    <xsl:if
      test="key('ident', substring(@xlink:href, 2))[self::rwt:administrative-advice|self::rwt:definition|self::rwt:caution] and
      pbs:effectivity/pbs:start">
      <xsl:text> </xsl:text>
      <xsl:value-of select="pbs:effectivity/pbs:start"/>
    </xsl:if>
    <xsl:apply-templates select="key('ident', substring(@xlink:href, 2))" mode="note-start"/>
  </xsl:template>
  <xsl:template match="rwt:restriction | rwt:circumstance" mode="note-start">
    <xsl:if test="pbs:effectivity/pbs:start">
      <xsl:text> </xsl:text>
      <xsl:value-of select="pbs:effectivity/pbs:start"/>
    </xsl:if>
    <xsl:apply-templates select="rwt:circumstance |
      rwt:circumstance-reference |
      rwt:administrative-advice |
      rwt:administrative-advice-reference |
      rwt:definition |
      rwt:definition-reference |
      rwt:caution |
      rwt:caution-reference"
      mode="note-start"/>
  </xsl:template>
  <xsl:template match="rwt:definition |
    rwt:administrative-advice |
    rwt:caution"
    mode="note-start">
    <xsl:if test="pbs:effectivity/pbs:start">
      <xsl:text> </xsl:text>
      <xsl:value-of select="pbs:effectivity/pbs:start"/>
    </xsl:if>
  </xsl:template>
  <xsl:template match="*|text()" mode="note-start"/>
  
  <xsl:template match="rwt:restriction-reference |
    rwt:circumstance-reference |
    rwt:administrative-advice-reference |
    rwt:definition-reference" mode="note-end">
    <xsl:if
      test="key('ident', substring(@xlink:href, 2))[self::rwt:administrative-advice|self::rwt:definition|self::rwt:caution] and
      pbs:effectivity/pbs:non-effective">
      <xsl:text> </xsl:text>
      <xsl:value-of select="pbs:effectivity/pbs:non-effective"/>
    </xsl:if>
    <xsl:apply-templates select="key('ident', substring(@xlink:href, 2))"
      mode="note-end"/>
  </xsl:template>
  <xsl:template match="rwt:restriction | rwt:circumstance" mode="note-end">
    <xsl:if test="pbs:effectivity/pbs:non-effective">
      <xsl:text> </xsl:text>
      <xsl:value-of select="pbs:effectivity/pbs:non-effective"/>
    </xsl:if>
    <xsl:apply-templates select="rwt:circumstance |
      rwt:circumstance-reference |
      rwt:administrative-advice |
      rwt:administrative-advice-reference |
      rwt:definition |
      rwt:definition-reference |
      rwt:caution |
      rwt:caution-reference"
      mode="note-end"/>
  </xsl:template>
  <xsl:template match="rwt:definition |
    rwt:administrative-advice |
    rwt:caution"
    mode="note-end">
    <xsl:if test="pbs:effectivity/pbs:non-effective">
      <xsl:text> </xsl:text>
      <xsl:value-of select="pbs:effectivity/pbs:non-effective"/>
    </xsl:if>
  </xsl:template>
  <xsl:template match="*|text()" mode="note-end"/>
</xsl:stylesheet>
